home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / DragWell.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  7.2 KB  |  267 lines  |  [TEXT/CWIE]

  1. // DragWell.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. /** View subclass that vends drag-and-droppable items.  Unless changed, the
  10.   * DragWell vends the same data each time the user clicks and drags from
  11.   * the DragWell.
  12.   */
  13. public class DragWell extends View implements DragSource {
  14.     Image        image;
  15.     String       dataType;
  16.     Object       data;
  17.     Border       border = BezelBorder.loweredBezel();
  18.     boolean      enabled = true;
  19.  
  20.     final static String         IMAGE_KEY = "image",
  21.                                 DATA_KEY = "data",
  22.                                 DATATYPE_KEY = "dataType",
  23.                                 ENABLED_KEY = "enabled",
  24.                                 BORDER_KEY = "border";
  25.  
  26.  
  27.     /* constructors */
  28.  
  29.     /** Constructs a DragWell with origin (0, 0) and zero width
  30.       * and height
  31.       */
  32.     public DragWell() {
  33.         this(0, 0, 0, 0);
  34.     }
  35.  
  36.     /** Constructs a DragWell with bounds <B>rect</B>.
  37.       */
  38.     public DragWell(Rect rect) {
  39.         this(rect.x, rect.y, rect.width, rect.height);
  40.     }
  41.  
  42.     /** Constructs a DragWell
  43.       * with bounds (<B>x</B>, <B>y</B>, <B>width</B>, <B>height</B>).
  44.       */
  45.     public DragWell(int x, int y, int width, int height) {
  46.         super(x, y, width, height);
  47.     }
  48.  
  49.     /** Overriden to return <b>false</b>. */
  50.     public boolean isTransparent() {
  51.         return false;
  52.     }
  53.  
  54.     /** Sets the DragWell's Image, the Image it displays within its bounds,
  55.       * and then calls its
  56.       * <B>draw()</B> method to redraw it.  If you do not want to immediately
  57.       * redraw the DragWell, you should first call its
  58.       * <B>disableDrawing()</B> method.
  59.       * @see View#disableDrawing
  60.       */
  61.     public void setImage(Image anImage) {
  62.         if (image != anImage) {
  63.             image = anImage;
  64.             draw();
  65.         }
  66.     }
  67.  
  68.     /** Returns the DragWell's Image.
  69.       * @see #setImage
  70.       */
  71.     public Image image() {
  72.         return image;
  73.     }
  74.  
  75.     /** Sets the type of the data represented by the DragWell's Image.
  76.       * @see DragSession#setDataType
  77.       */
  78.     public void setDataType(String dataType) {
  79.         this.dataType = dataType;
  80.     }
  81.  
  82.     /** Returns the type of the data represented by the DragWell's Image.
  83.       * @see #setDataType
  84.       */
  85.     public String dataType() {
  86.         return dataType;
  87.     }
  88.  
  89.     /** Sets the object represented by the DragWell's Image.  When the user
  90.       * clicks the DragWell, the DragWell initiates a DragSession displaying
  91.       * the DragWell's Image and containing the DragWell's data.
  92.       */
  93.     public void setData(Object anObject) {
  94.         data = anObject;
  95.     }
  96.  
  97.     /** Returns the object represented by the DragWell's Image.
  98.       * @see #setData
  99.       */
  100.     public Object data() {
  101.         return data;
  102.     }
  103.  
  104.     /** Disables or enables the DragWell, and calls <b>draw()</b> to redraw it.
  105.       * A disabled DragWell does not respond to mouse clicks.
  106.       * If you do not want to immediately
  107.       * redraw the DragWell, you should first call its
  108.       * <B>disableDrawing()</B> method.
  109.       * @see View#disableDrawing
  110.       */
  111.     public void setEnabled(boolean flag) {
  112.         if (enabled != flag) {
  113.             enabled = flag;
  114.  
  115.             draw();
  116.         }
  117.     }
  118.  
  119.     /** Returns <B>true</b> if the DragWell is enabled.
  120.       * @see #setEnabled
  121.       */
  122.     public boolean isEnabled() {
  123.         return enabled;
  124.     }
  125.  
  126.     /** Sets the DragWell's Border.
  127.       * @see Border
  128.       */
  129.     public void setBorder(Border newBorder) {
  130.         if (newBorder == null)
  131.             newBorder = EmptyBorder.emptyBorder();
  132.  
  133.         border = newBorder;
  134.     }
  135.  
  136.     /** Returns the DragWell's Border.
  137.       * @see #setBorder
  138.       */
  139.     public Border border() {
  140.         return border;
  141.     }
  142.  
  143.     /** Initiates a DragSession where the user can drag the DragWell's
  144.       * data, unless the DragWell is disabled (in which case nothing
  145.       * happens).
  146.       * @see #setEnabled
  147.       * @return <b>false</b> if disabled, <b>true</b> otherwise.
  148.       */
  149.     public boolean mouseDown(MouseEvent event) {
  150.         Image        theImage;
  151.         Rect         tmpRect;
  152.  
  153.         if (!enabled) {
  154.             return false;
  155.         }
  156.  
  157.         theImage = image();
  158.         if (theImage == null) {
  159.             return false;
  160.         }
  161.  
  162.         tmpRect = new Rect((width() - theImage.width()) / 2,
  163.                            (height() - theImage.height()) / 2,
  164.                            theImage.width(), theImage.height());
  165.  
  166.         if (!tmpRect.contains(event.x, event.y)) {
  167.             return false;
  168.         }
  169.  
  170.         new DragSession(this, theImage,
  171.                         tmpRect.x, tmpRect.y,
  172.                         event.x, event.y,
  173.                         dataType(), data);
  174.  
  175.         return true;
  176.     }
  177.  
  178.     /** Draws the DragWell by drawing its Border, filled with the DragWell's
  179.       * Image.
  180.       */
  181.     public void drawView(Graphics g) {
  182.         Image        theImage;
  183.  
  184.         g.setColor(Color.lightGray);
  185.         g.fillRect(0, 0, width(), height());
  186.  
  187.         theImage = image();
  188.         if (theImage != null) {
  189.             theImage.drawCentered(g, 0, 0, width(), height());
  190.         }
  191.  
  192.         border.drawInRect(g, 0, 0, width(), height());
  193.     }
  194.  
  195. /* drag source */
  196.  
  197.     /** DragSource support method.
  198.       * @see DragSource#sourceView
  199.       */
  200.     public View sourceView(DragSession session) {
  201.         return this;
  202.     }
  203.  
  204.     /** DragSource support method.
  205.       * @see DragSource#dragAccepted
  206.       */
  207.     public void dragWasAccepted(DragSession session) {
  208.     }
  209.  
  210.     /** DragSource support method.
  211.       * @see DragSource#dragRejected
  212.       */
  213.     public boolean dragWasRejected(DragSession session) {
  214.         return true;
  215.     }
  216.  
  217. /* archiving */
  218.  
  219.  
  220.     /** Describes the DragWell class' information.
  221.       * @see Codable#describeClassInfo
  222.       */
  223.     public void describeClassInfo(ClassInfo info) {
  224.         super.describeClassInfo(info);
  225.  
  226.         info.addClass("netscape.application.DragWell", 1);
  227.         info.addField(IMAGE_KEY, OBJECT_TYPE);
  228.         info.addField(DATA_KEY, OBJECT_TYPE);
  229.         info.addField(DATATYPE_KEY, STRING_TYPE);
  230.         info.addField(ENABLED_KEY, BOOLEAN_TYPE);
  231.         info.addField(BORDER_KEY, OBJECT_TYPE);
  232.     }
  233.  
  234.     /** Encodes the DragWell instance.
  235.       * @see Codable#encode
  236.       */
  237.     public void encode(Encoder encoder) throws CodingException {
  238.         super.encode(encoder);
  239.  
  240.         encoder.encodeObject(IMAGE_KEY, image);
  241.         encoder.encodeObject(DATA_KEY, (Codable)data);
  242.         encoder.encodeString(DATATYPE_KEY, dataType);
  243.  
  244.         encoder.encodeBoolean(ENABLED_KEY, enabled);
  245.  
  246.         if (border instanceof EmptyBorder)
  247.             encoder.encodeObject(BORDER_KEY, null);
  248.         else
  249.             encoder.encodeObject(BORDER_KEY, border);
  250.     }
  251.  
  252.     /** Decodes the DragWell instance.
  253.       * @see Codable#decode
  254.       */
  255.     public void decode(Decoder decoder) throws CodingException {
  256.         super.decode(decoder);
  257.  
  258.         image = (Image)decoder.decodeObject(IMAGE_KEY);
  259.         data = decoder.decodeObject(DATA_KEY);
  260.         dataType = decoder.decodeString(DATATYPE_KEY);
  261.  
  262.         enabled = decoder.decodeBoolean(ENABLED_KEY);
  263.  
  264.         setBorder((Border)decoder.decodeObject(BORDER_KEY));
  265.     }
  266. }
  267.